package com.etl.dataflow.common.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.etl.dataflow.common.response.Code;

/**
 * @author dx
 * @since 2022/4/21
 */
public class ResponseUtil {
    private ResponseUtil() {
    }

    public static JSONObject toJson(String body) {
        return JSONUtil.parseObj(body);
    }

    public static boolean isSuccess(String body) {
        JSONObject json = toJson(body);
        return isSuccess(json);
    }

    public static boolean isFailed(String body) {
        return !isSuccess(body);
    }

    public static boolean isFailed(JSONObject json) {
        return !isSuccess(json);
    }

    public static boolean codeEquals(String body, Code code) {
        JSONObject json = toJson(body);
        return codeEquals(json, code);
    }

    public static boolean codeEquals(JSONObject json, Code code) {
        Integer responseCode = (Integer) json.get("code");
        return responseCode != null && responseCode.equals(code.getCode());
    }

    public static boolean isSuccess(JSONObject json) {
        return codeEquals(json, Code.SUCCESS);
    }

}
